import xpMtgSDK from '../../services/mtgsdk/xpMtgSdk.umd'

Page({
  data: {
    isVoted: false,
    items: [{
        value: '1',
        name: '',
        src: "https://cdn-resource.xipin.top/mtg/mini-vote-longzhu2/1.jpg",
        checked: false,
        votes: 0,
      },
      {
        value: '2',
        name: '',
        src: "https://cdn-resource.xipin.top/mtg/mini-vote-longzhu2/2.jpg",
        checked: false,
        votes: 0,
      },
      {
        value: '3',
        name: '',
        src: "https://cdn-resource.xipin.top/mtg/mini-vote-longzhu2/3.jpg",
        checked: false,
        votes: 0,
      },
      {
        value: '5',
        name: '',
        src: "https://cdn-resource.xipin.top/mtg/mini-vote-longzhu2/5.jpg?v=1",
        checked: false,
        votes: 0,
      },
      {
        value: '6',
        name: '',
        src: "https://cdn-resource.xipin.top/mtg/mini-vote-longzhu2/6.jpg",
        checked: false,
        votes: 0,
      },
      {
        value: '7',
        name: '',
        src: "https://cdn-resource.xipin.top/mtg/mini-vote-longzhu2/7.jpg",
        checked: false,
        votes: 0,
      },
      {
        value: '8',
        name: '',
        src: "https://cdn-resource.xipin.top/mtg/mini-vote-longzhu2/8.jpg",
        checked: false,
        votes: 0,
      },
    ],
    src: "",
  },
  onLoad() {
    wx.hideHomeButton();
  },
  radioChange: function (e) {
    const items = this.data.items
    for (let i = 0, len = items.length; i < len; ++i) {
      items[i].checked = items[i].value === e.detail.value
    }
  },
  selectOne: function (e) {
    const items = this.data.items
    let value = e.currentTarget.dataset.value;
    for (let i = 0, len = items.length; i < len; ++i) {
      items[i].checked = items[i].value === value
      items[i].border = items[i].value === value ? 'border: 1px solid red;' : ''
    }

    this.setData({
      items
    });
  },

  vote: function (e) {
    const items = this.data.items;
    let index = items.findIndex(item => item.checked == true);
    if (index == -1) {
      wx.showToast({
        icon: 'none',
        title: '请先选择你喜欢的一项',
        duration: 2000
      });
      return;
    }

    const _this = this
    xpMtgSDK.send('/api/mtg/vote/vote', {
      vote_item_id: items[index].value
    }, function (res) {
      let msg = "投票成功！";
      if (res.code === 0) {
        msg = "请勿重复投票";
      }

      wx.showToast({
        icon: 'none',
        title: '投票成功！',
        duration: 1000
      });

      const data = res.data;

      if (data.is_vote == 1) {
        setTimeout(() => {
          _this.setData({
            isVoted: true,
          })
        }, 1200);
      }

      let vote_list = data.vote_list
      for (let i = 0, len = items.length; i < len; ++i) {
        let v_index = vote_list.findIndex(item => item.item_id == items[i].value)
        if (v_index != -1) {
          items[i].votes = vote_list[v_index].num;
        }
      }

      _this.setData({
        items
      });

      return;
    })
  }
});
